home *** CD-ROM | disk | FTP | other *** search
- Path: news.umbc.edu!not-for-mail
- From: schlein@umbc.edu (Jonas J. Schlein)
- Newsgroups: comp.lang.c
- Subject: Re: fclose() and remove() within a funct'n
- Date: 18 Feb 1996 17:06:06 -0500
- Organization: University of Maryland Baltimore County
- Message-ID: <4g87se$rt0@umbc9.umbc.edu>
- References: <4fo8ps$3v6@milo.freenet.vancouver.bc.ca>
- NNTP-Posting-Host: umbc9.umbc.edu
- NNTP-Posting-User: schlein
-
- Patrick Wong <zipz@opus.freenet.vancouver.bc.ca> wrote:
- |> I'm writing a function that is supposed to close and remove temporary files
- |> on a DOS machine. I'm posting this in c.l.c because it's a C question as
- |> well as a DOS question.
-
- As long as it's portable across all ANSI C compilers then it's perfectly
- acceptable to post it here.
-
- |> I am using the atexit() function to close the temporary file before
- |> exiting the function. Yet the atexit() function will not allow me to
- |> pass a pointer to the file that I want to close (and delete). The
- |> function MUST be responsible for closing and deleting all temporary files
- |> it creates upon PROGRAM termination (hence the atexit() function).
-
- I've never used atexit() before, but it is my understanding that upon
- program exit whatever was given to atexit() is executed. Assuming normal
- program exit. However the function passed to atexit() must take and
- return void.
-
- |> Here is the function:
- |>
- |> FILE *tempname(char *filename, char *mode)
- |> {
- |> FILE *fptemp;
- |> char *ptemp;
- |> const char fnlen = 13;
- |>
- |> if ((ptemp = (char *)malloc(fnlen * sizeof(char))) == NULL)
-
- The cast is not necessary and if you do not #include <stdlib.h> then
- the cast will actually hide a flaw in your program. The sizeof() is not
- needed either since a char is guaranteed to be 1 in ANSI C.
-
- |> return NULL;
- |> ptemp = (filename == NULL) ? tmpnam(NULL) : tmpnam(filename);
- |> return (((fptemp = fopen(filename, mode)) == NULL) ? NULL : fptemp);
-
- Wow I thought I was reading LISP for a second. Actually what you have here is
- precisely equivalent to:
-
- return (fopen (filename, mode));
-
- Yes, you do check against NULL but handle it by just returning NULL.
-
- |> }
- |> /* so how do I add the functionality descrbed above? */
- |> /* I believe tmpnam() if a non-ansi function ... is it? */
-
- You're on safe ground...Both tmpfile() and tmpnam() are ANSI C so long
- as you #include <stdio.h>.
-
- Given this you may want to use tmpfile() which returns a FILE * with wb+
- mode that is guaranteed to be removed upon being either closed or upon
- program exit.
-
- Maybe I didn't understand your question...Please elaborate if I'm off base.
- --
- "If it wasn't for C, we would be using BASI, PASAL, and OBOL."
-
- Jonas J. Schlein (schlein@gl.umbc.edu)
-